/**
 * Created by lining on 18/6/25.
 */

var mysql = require('mysql');
class Database
{
    constructor()
    {
        this.connection = mysql.createConnection({
            host:'127.0.0.1',
            user:'root',
            password:'12345678',
            database:'cloudnote',
            port:3306,
            charset:'UTF8_GENERAL_CI'
        })
    }
    //  向note表中插入云笔记数据
    addNote(req,res)
    {
        this.connection.query('insert into note set ?', {user_id:req.query.id,name:req.query.name,code:req.query.code},function(err,result){
            if(err)
            {
                console.log(err);
            }
            else
            {
                console.log(result);
                res.send(result);
            }
        })
    }

    //  更新云笔记
    updateNote(req,res)
    {
        this.connection.query('update note set ? where id=' + req.query.sqlid,{name:req.query.name,code:req.query.code},function(err,result){
            if(err)
            {
                console.log(err);
            }
            else
            {
                res.send(result);
            }
        })

    }
    //  根据登录用户的id获取云笔记的name列表
    getNoteList(req,res)
    {
        this.connection.query("select * from note where ?",{user_id:req.query.id},function(err,result){
            if(err)
            {
                console.log(err);
            }
            else
            {
                console.log(result);
                 res.send(result);
            }
        })
    }
}
module.exports = Database;